package com.txby.platformApi.component.web;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.engine.transaction.jta.platform.internal.SynchronizationRegistryBasedSynchronizationStrategy;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.AddGroup;
import com.txby.platformCore.entity.SysOption;

/**
 * 选项
 */
@RestController
@RequestMapping("/sys/optionval")
public class OptionValController  extends CommonController {
	
	/**
	 * 所有选项列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:optionval:list")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String join="";
		String sql="SELECT * FROM "+this.mBaseDao.tableName(SysOption.class) ;
	/*	if(getUserId() != Constant.SUPER_ADMIN){
//			params.put("createUserId", getUserId());
			 join=getJoinSqlBracket();
			 sql+=" and name in "+getJoinSqlBracket();
		}*/
		String value=params.get("value")==null?"":params.get("value").toString();
		if(!ChkUtil.isEmptyAllObject(value)){
			if (value.contains("%") || value.contains("_")) {
				value = value.trim().replace("%", "\\%").replace("_", "\\_");
				value = "%" + value + "%";
				sql += " where value like #{pojo.value} escape '\\'";
				params.put("value", "%" + value.trim() + "%");
			} else {
				value = "%" + value.trim() + "%";
				sql += " where value like #{pojo.value} ";
				params.put("value", "%" + value.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by topicId ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	
 
	
	/**
	 * 选项信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:optionval:info")
	public R info(@PathVariable("id") Long id){
		String sql="SELECT * FROM  "+this.mBaseDao.tableName(SysOption.class)+" WHERE id_="+id+"" ;
		Map optionval =  (Map) this.mBaseDao.findBySqlUniqueObject(sql, Map.class, null);
		return R.ok().put("optionval", optionval);
	}
	
	/**
	 * 保存选项
	 */
	@SysLog("保存选项")
	@RequestMapping("/save")
	@RequiresPermissions("sys:optionval:save")
	/*public R save(@RequestBody SysOption optionval){*/
	public R save(@RequestBody SysOption optionval){
		try{
			
			//String topicId = optionval.get("topicId").toString();
			//String value =optionval.get("value").toString();
			
		ValidatorUtils.validateEntity(optionval);
		String sql="select * from "+this.mBaseDao.tableName(SysOption.class)+" where value='"+optionval.getValue()+"'";
		SysOption old=(SysOption) this.mBaseDao.findBySqlUniqueObject(sql, SysOption.class, null);
		 if(old!=null){
			 return R.ok().error("选项内容重复");
		 }
			this.mBaseDao.saveObject(optionval);
		}catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	/**
	 * 修改选项
	 */
	@SysLog("修改选项")
	@RequestMapping("/update")
	@RequiresPermissions("sys:optionval:update")
	public R update(@RequestBody  SysOption optionval){
		try{
		ValidatorUtils.validateEntity(optionval);
		   String sql="select * from "+this.mBaseDao.tableName(SysOption.class)+" where value='"+optionval.getValue()+"' and id_!="+optionval.getId_()+" ";
		SysOption old=(SysOption) this.mBaseDao.findBySqlUniqueObject(sql, SysOption.class, null);
		 if(old!=null){
			 return R.ok().error("选项名称重复");
		 }else{
			 old=(SysOption) this.mBaseDao.findBySqlUniqueObject("select * from "+this.mBaseDao.tableName(SysOption.class)+" where   id_="+optionval.getId_(), SysOption.class, null);
			 this.mBaseDao.updateObject(optionval);
		 }
		}catch (Exception e) {
			return R.error(5000,e.getMessage());
		}
		return R.ok();
	}
	
	/**
	 * 删除选项
	 */
	@SysLog("删除选项")
	@RequestMapping("/delete")
	@RequiresPermissions("sys:optionval:delete")
	public R delete(@RequestBody Long[] ids){
		 for(Long id:ids){
		    	this.mBaseDao.executeSql(" delete from "+this.mBaseDao.tableName(SysOption.class)+" where id_="+id+"", null);
		  }
			return R.ok();
	}
	
	/**
	 * 查询题干
	 */
	@RequestMapping("/queryquestionList")
	public R querysingleList(){
		String sql="select topic text,id_ value from t_txby_bd_sysCusTopic where type <> 3 ";
		List queryquestionList=this.mBaseDao.findBySql(sql, Map.class, null);
		return R.ok().put("questionList", queryquestionList);
	}
	
	
}
